﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Xml;
using System.IO;
using System.Threading;

namespace DeloUtils.Migra
{
    public delegate void ProgressHandler(int progress);
    public delegate void ProgressMessage(string progress);
    class ExportDocuments
    {
        public static LocalDBCompact localdb;
        public static event ProgressHandler CurrentRows;
        //public static event ProgressMessage CurrentMessage;
        public static int MaxThread = 2;
        public static int year = 0;
        public static bool dontReloadXml = true;

        public static string GetVersion(OleDbConnection connection)
        {
            if (connection == null) return "";
            string sql = "SELECT MAX(RELISE_NUM) FROM PATCH_HISTORY";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            object res = cmd.ExecuteScalar();
            return ((res!=null)?(res):0).ToString();
        }

        public static Int32 CountRows(OleDbConnection connection)
        {
            string version = GetVersion(connection);
            switch (version)
            {
                case "7005002":
                    return CountRows754mss(connection);
                case "11000005":
                    return CountRows12mss(connection);
                case "12000000":
                    return CountRows12mss(connection);
                default: return 0;
            }
        }

        public static void Execute(String Folder, OleDbConnection connection)
        {
            string version = GetVersion(connection);
            switch (version)
            {
                case "7005002":
                    Execute754mss(Folder,connection);
                    break;
                case "12000000":
                    Execute12mss(Folder, connection);
                    break;
                case "11000005":
                    Execute12mss(Folder, connection);
                    break;
            }

        }

        #region 12 Версия Microsoft SQL Server
        public static Int32 CountRows12mss(OleDbConnection connection) 
        {
            String sql = "SELECT count(*) FROM DOC_RC";
            OleDbCommand cmdDocuments = new OleDbCommand(sql, connection);
            return (Int32)cmdDocuments.ExecuteScalar();
        }
        public static void Execute12mss(String Folder, OleDbConnection connection)
        {
            String sql = "SELECT KIND_DOC,ISN_DOC,ISN_CARD_REG,DUE_DOCGROUP,ORDER_NUM,FREE_NUM,DOC_DATE, "
                       + "SECURLEVEL,CONSISTS,SPECIMEN,PLAN_DATE,FACT_DATE,CONTROL_STATE,ANNOTAT,NOTE, "
                       + "ISN_DELIVERY,ISCOLLECTIVE,ANONIM,DUE_PERSON_EXE FROM DOC_RC ORDER BY ISN_DOC";
            OleDbCommand cmdDocuments = new OleDbCommand(sql, connection);
            OleDbDataReader drDocuments = cmdDocuments.ExecuteReader();
            CurrentRows(0);
            int i = 0;

            //StreamWriter _file=File.CreateText(Folder + "\\protocol.log");
            while (drDocuments.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                i++;
                CurrentRows(i);
                if ((dontReloadXml) && (File.Exists(Folder + "\\" + drDocuments.GetInt32(1).ToString() + ".xml"))) continue; //Проверка на наличие выгруженного документа в папке.
                XmlDocument doc = new XmlDocument();
                XmlNode root = doc.CreateElement("root", "");
                XmlNode rc = doc.CreateElement("rc", "");
                #region RC-ATTR
                XmlAttribute kind = doc.CreateAttribute("kind");
                kind.Value = drDocuments.GetInt16(0).ToString();
                rc.Attributes.Append(kind);
                XmlAttribute isn = doc.CreateAttribute("isn");
                isn.Value = drDocuments.GetInt32(1).ToString();
                rc.Attributes.Append(isn);
                XmlNode tag;

                if (!drDocuments.IsDBNull(2))
                {

                    tag = GetDepartment12mss(connection,doc,drDocuments.GetInt32(2),"CARD");
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(3))
                {
                    tag = GetDocgroup12mss(connection,doc,drDocuments.GetString(3));
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(4))
                {
                    tag = doc.CreateElement("ORDER_NUM", "");
                    tag.InnerText = drDocuments.GetInt32(4).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(5))
                {
                    tag = doc.CreateElement("FREE_NUM", "");
                    tag.InnerText = drDocuments.GetString(5);
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(6))
                {
                    tag = doc.CreateElement("DOC_DATE", "");
                    tag.InnerText = drDocuments.GetValue(6).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(7))
                {
                    tag = GetSecurlevel12mss(connection,doc,drDocuments.GetInt32(7));
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(8))
                {
                    tag = doc.CreateElement("CONSISTS", "");
                    tag.InnerText = drDocuments.GetString(8);
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(9))
                {
                    tag = doc.CreateElement("SPECIMEN", "");
                    tag.InnerText = drDocuments.GetString(9);
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(10))
                {
                    tag = doc.CreateElement("PLAN_DATE", "");
                    tag.InnerText = drDocuments.GetValue(10).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(11))
                {
                    tag = doc.CreateElement("FACT_DATE", "");
                    tag.InnerText = drDocuments.GetValue(11).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(12))
                {
                    tag = doc.CreateElement("CONTROL_STATE", "");
                    tag.InnerText = drDocuments.GetInt16(12).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(13))
                {
                    tag = doc.CreateElement("ANNOTAT", "");
                    tag.InnerText = drDocuments.GetString(13);
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(14))
                {
                    tag = doc.CreateElement("NOTE", "");
                    tag.InnerText = drDocuments.GetString(14);
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(15))
                {
                    tag = GetDelivery12mss(connection, doc, drDocuments.GetInt32(15));
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(16))
                {
                    tag = doc.CreateElement("ISCOLLECTIVE", "");
                    tag.InnerText = drDocuments.GetInt16(16).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(17))
                {
                    tag = doc.CreateElement("ANONIM", "");
                    tag.InnerText = drDocuments.GetInt16(17).ToString();
                    rc.AppendChild(tag);
                }

                if (!drDocuments.IsDBNull(18))
                {
                    tag = GetDepartment12mss(connection, doc, drDocuments.GetString(18), "PERSON_EXE");
                    rc.AppendChild(tag);
                }
                #endregion RC-ATTR
                tag = GetArValues12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetResolution12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetRubric12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetLetter12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetWho12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetSign12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetVisa12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetSoisp12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetLink12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetJournal12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetSend12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                tag = GetCorresp12mss(connection, doc, drDocuments.GetInt32(1));
                rc.AppendChild(tag);
                root.AppendChild(rc);
                doc.AppendChild(root);
                doc.Save(Folder + "\\" + isn.Value + ".xml");
                if ((localdb.Connection.State & ConnectionState.Open) == ConnectionState.Open)
                {
                    Data _data = new Data("RC", DateTime.Now, drDocuments.GetInt32(1), string.Empty);
                    _data.SaveDB(localdb);
                    //_file.WriteLine(_data);
                }
            }

            drDocuments.Close();
           // _file.Close();
        }
        private static XmlNode GetDepartment12mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT DCHI.DUE, DCHI.ISN_NODE, DCHI.ISN_HIGH_NODE, DCHI.IS_NODE, DCHI.CLASSIF_NAME, ";
            sql += "DCHI.SURNAME, DCHI.PROTECTED, DCHI.DELETED, DCHI.POST_H, DCHI.DEPARTMENT_INDEX, ";
            sql += "DCHI.CARD_FLAG, DCHI.CARD_NAME, DCHI.NOTE, DPAR.CLASSIF_NAME AS PARENTNAME, ";
            sql += "DPAR.CARD_FLAG AS PARENTCARDFLAG, DPAR.CARD_NAME AS PARENTCARDNAME FROM DEPARTMENT AS DCHI ";
            sql += "INNER JOIN DEPARTMENT AS DPAR ON DCHI.ISN_HIGH_NODE = DPAR.ISN_NODE WHERE DCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("SURNAME");
                    tag.InnerText = dr.GetString(5);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("PROTECTED");
                    tag.InnerText = dr.GetInt16(6).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("POST_H");
                    tag.InnerText = dr.GetInt16(8).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("DEPARTMENT_INDEX");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CARD_FLAG");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("CARD_NAME");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }

                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(13);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("CARD_FLAG");
                    tag.InnerText = dr.GetInt16(14).ToString();
                    parent.AppendChild(tag);
                }

                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("CARD_NAME");
                    tag.InnerText = dr.GetString(15);
                    parent.AppendChild(tag);
                }
                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDepartment12mss(OleDbConnection connection, XmlDocument doc, String due, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT DCHI.DUE, DCHI.ISN_NODE, DCHI.ISN_HIGH_NODE, DCHI.IS_NODE, DCHI.CLASSIF_NAME, ";
            sql += "DCHI.SURNAME, DCHI.PROTECTED, DCHI.DELETED, DCHI.POST_H, DCHI.DEPARTMENT_INDEX, ";
            sql += "DCHI.CARD_FLAG, DCHI.CARD_NAME, DCHI.NOTE, DPAR.CLASSIF_NAME AS PARENTNAME, ";
            sql += "DPAR.CARD_FLAG AS PARENTCARDFLAG, DPAR.CARD_NAME AS PARENTCARDNAME FROM DEPARTMENT AS DCHI ";
            sql += "LEFT OUTER JOIN DEPARTMENT AS DPAR ON DCHI.ISN_HIGH_NODE = DPAR.ISN_NODE WHERE DCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("SURNAME");
                    tag.InnerText = dr.GetString(5);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("PROTECTED");
                    tag.InnerText = dr.GetInt16(6).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("POST_H");
                    tag.InnerText = dr.GetInt16(8).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("DEPARTMENT_INDEX");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CARD_FLAG");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("CARD_NAME");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }

                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(13);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("CARD_FLAG");
                    tag.InnerText = dr.GetInt16(14).ToString();
                    parent.AppendChild(tag);
                }

                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("CARD_NAME");
                    tag.InnerText = dr.GetString(15);
                    parent.AppendChild(tag);
                }
                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDocgroup12mss(OleDbConnection connection, XmlDocument doc, String due)
        {
            XmlNode result = doc.CreateElement("DOCGROUP");
            String sql = "SELECT DGCHI.DUE, DGCHI.ISN_NODE, DGCHI.ISN_HIGH_NODE, DGCHI.IS_NODE, DGCHI.CLASSIF_NAME, "; 
            sql+="DGCHI.PROTECTED, DGCHI.DELETED, DGCHI.RC_TYPE, DGCHI.DOCGROUP_INDEX, DGCHI.NOTE, DGPAR.CLASSIF_NAME, ";
            sql += "DGPAR.RC_TYPE FROM DOCGROUP_CL AS DGCHI INNER JOIN DOCGROUP_CL AS DGPAR ON DGCHI.ISN_HIGH_NODE = DGPAR.ISN_NODE ";
            sql += "WHERE DGCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("PROTECTED");
                    tag.InnerText = dr.GetInt16(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(6).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("RC_TYPE");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("DOCGROUP_INDEX");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(10);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("RC_TYPE");
                    tag.InnerText = dr.GetInt16(11).ToString();
                    parent.AppendChild(tag);
                }
                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetSecurlevel12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SECURLEVEL");
            String sql = "SELECT SECURLEVEL, GRIF_NAME FROM SECURITY_CL ";
            sql += "WHERE SECURLEVEL=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(1))
                {
                    result.InnerText = dr.GetString(1);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDelivery12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("DELIVERY");
            String sql = "SELECT  ISN_LCLASSIF, CLASSIF_NAME FROM DELIVERY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(1))
                {
                    result.InnerText = dr.GetString(1);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetResolution12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("RESOLUTIONS");
            String sql ="SELECT ISN_RESOLUTION, KIND_RESOLUTION, ISN_PARENT_RESOLUTION, ";
            sql += "LAYER, WEIGHT, ITEM_NUMBER, DUE, RESOLUTION_TEXT, ISN_CATEGORY,CONFIDENTIONAL, ";
            sql += "RESOLUTION_DATE, SEND_DATE, PLAN_DATE, INTERIM_DATE, FACT_DATE, DELTA_DATE, DUE_CONTROLLER, ";
            sql += "CONTROL_STATE, ISN_STATUS_EXEC, SUMMARY, CASCADE_CONTROL, RESUME, LEFT_REZOLUTION, ";
            sql += "NOTE FROM RESOLUTION WHERE ISN_REF_DOC=" + isn.ToString();
            sql += " ORDER BY LAYER, WEIGHT, RESOLUTION_DATE";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("RESOLUTION");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("KIND_RESOLUTION");
                    tag.InnerText = dr.GetInt16(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_PARENT_RESOLUTION");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("ITEM_NUMBER");
                    tag.InnerText = dr.GetString(5);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag=GetDepartment12mss(connection,doc,dr.GetString(6),"DUE");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("RESOLUTION_TEXT");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = GetResolutionCategory12mss(connection,doc,dr.GetInt32(8));
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("CONFIDENTIONAL");
                    tag.InnerText = dr.GetInt16(9).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("RESOLUTION_DATE");
                    tag.InnerText = dr.GetValue(10).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("SEND_DATE");
                    tag.InnerText = dr.GetValue(11).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("PLAN_DATE");
                    tag.InnerText = dr.GetValue(12).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("INTERIM_DATE");
                    tag.InnerText = dr.GetValue(13).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("FACT_DATE");
                    tag.InnerText = dr.GetValue(14).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("DELTA_DATE");
                    tag.InnerText = dr.GetInt32(15).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(16), "DUE_CONTROLLER");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("CONTROL_STATE");
                    tag.InnerText = dr.GetInt16(17).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag=GetResolutionStatus12mss(connection,doc,dr.GetInt32(18));
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(19))
                {
                    tag = doc.CreateElement("SUMMARY");
                    tag.InnerText = dr.GetString(19);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(20))
                {
                    tag = doc.CreateElement("CASCADE_CONTROL");
                    tag.InnerText = dr.GetInt16(20).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(21))
                {
                    tag = doc.CreateElement("RESUME");
                    tag.InnerText = dr.GetString(21);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(22))
                {
                    tag = doc.CreateElement("LEFT_REZOLUTION");
                    tag.InnerText = dr.GetInt16(22).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(23))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(23);
                    res.AppendChild(tag);
                }
                tag = GetReply12mss(connection, doc, dr.GetInt32(0));
                res.AppendChild(tag);
                result.AppendChild(res);
            }

            return result;
        }
        private static XmlNode GetUser12mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT DUE_DEP, SURNAME_PATRON, CLASSIF_NAME, NOTE, ORACLE_ID FROM USER_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(0))
                {
                    tag = GetDepartment12mss(connection,doc,dr.GetString(0),"DUE_DEP");
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("SURNAME_PATRON");
                    tag.InnerText = dr.GetString(1);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(2);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(3);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("ORACLE_ID");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetResolutionCategory12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("ISN_CATEGORY");
            String sql = "SELECT CLASSIF_NAME FROM RESOLUTION_CATEGORY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetResolutionStatus12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("STATUS_EXEC");
            String sql = "SELECT CLASSIF_NAME FROM STATUS_EXEC_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetReplyStatus12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("STATUS_REPLY");
            String sql = "SELECT CLASSIF_NAME FROM STATUS_REPLY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetReply12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("REPLYS");
            String sql = "SELECT  ISN_REPLY, DUE, MAIN_FLAG, REPLY_TEXT, ";
            sql += "REPLY_DATE, ISN_STATUS_REPLY FROM REPLY ";
            sql += "WHERE ISN_RESOLUTION=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            XmlNode res;
            while (dr.Read())
            {
                res = doc.CreateElement("REPLY");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(1), "DUE");
                    res.AppendChild(tag);
                }
                    tag = doc.CreateElement("ISN_RESOLUTION");
                    tag.InnerText = isn.ToString();
                    res.AppendChild(tag);
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("MAIN_FLAG");
                    tag.InnerText = dr.GetInt16(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("REPLY_TEXT");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("REPLY_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetReplyStatus12mss(connection,doc,dr.GetInt32(5));
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetRubric12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode res = doc.CreateElement("REF_RUBRICS");
            String sql = "SELECT RF.ISN_REF_RUBRIC, RF.ORDERNUM, ";
            sql += "RCHI.DUE, RCHI.ISN_NODE, RCHI.LAYER, RCHI.IS_NODE, RCHI.WEIGHT, RCHI.CLASSIF_NAME, RCHI.RUBRIC_CODE, ";
            sql += "RCHI.NOTE, RPAR.ISN_NODE, RPAR.CLASSIF_NAME, RPAR.RUBRIC_CODE, RPAR.NOTE FROM ";
            sql += "RUBRIC_CL AS RCHI INNER JOIN RUBRIC_CL AS RPAR ON RCHI.ISN_HIGH_NODE = RPAR.ISN_NODE INNER JOIN ";
            sql += "REF_RUBRIC AS RF ON RCHI.DUE = RF.DUE_RUBRIC WHERE RF.ISN_REF_DOC=" + isn.ToString() + " ORDER BY RF.ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode result = doc.CreateElement("REF_RUBRIC");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(1).ToString();
                    result.AppendChild(tag);
                }
                XmlNode rubric = doc.CreateElement("RUBRIC");
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(2);
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(5).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(6).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(7);
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("RUBRIC_CODE");
                    tag.InnerText = dr.GetString(8);
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(9);
                    rubric.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(10).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(11);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("RUBRIC_CODE");
                    tag.InnerText = dr.GetString(12);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    parent.AppendChild(tag);
                }
                rubric.AppendChild(parent);
                result.AppendChild(rubric);
                res.AppendChild(result);
            }
            dr.Close();
            return res;
        }

        private static XmlNode GetArValues12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("AR_RC_VALUE");
            String sql = "SELECT * FROM AR_RC_VALUE WHERE ISN_RC=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                for (int i = 2; i < dr.FieldCount; i++)
                {
                    if (!dr.IsDBNull(i))
                    {
                        XmlNode tag = doc.CreateElement(dr.GetName(i));
                        tag.InnerText = dr.GetValue(i).ToString();
                        result.AppendChild(tag);
                    }
                }
            }
            dr.Close();
            return result;
        }

        private static XmlNode GetLetter12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            String sql = "SELECT RFL.ISN_REF_LETTER, RFL.NEEDANSWER, RFL.ORDERNUM, ";
            sql+="RFL.TEL_NUM, CIT.ISN_CITIZEN, CIT.CITIZEN_SURNAME, CIT.CITIZEN_CITY, CIT.ZIPCODE, CIT.CITIZEN_ADDR, CIT.ISN_REGION, ";
            sql+="CIT.DELETED, CIT.SEX, CIT.PHONE, CIT.INN, CIT.E_MAIL, CIT.NOTE FROM REF_LETTER AS RFL INNER JOIN CITIZEN AS CIT ON ";
            sql+="RFL.ISN_CITIZEN = CIT.ISN_CITIZEN WHERE RFL.ISN_DOC_INP=" + isn.ToString() + " ORDER BY RFL.ORDERNUM";
            XmlNode result = doc.CreateElement("REF_LETTERS");
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_LETTER");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.InnerText = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                #region REF_LETTER;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("NEEDANSWER");
                    tag.InnerText = dr.GetInt16(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("TEL_NUM");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                #endregion REF_LETTER
                #region CITIZEN
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("ISN_CITIZEN");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("CITIZEN_SURNAME");
                    tag.InnerText = dr.GetString(5);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CITIZEN_CITY");
                    tag.InnerText = dr.GetString(6);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("CITIZEN_ADDR");
                    tag.InnerText = dr.GetString(8);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = GetRegion12mss(connection,doc,dr.GetInt32(9));
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("SEX");
                    tag.InnerText = dr.GetInt16(11).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("PHONE");
                    tag.InnerText = dr.GetString(12);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("INN");
                    tag.InnerText = dr.GetString(13);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("E_MAIL");
                    tag.InnerText = dr.GetString(14);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(15);
                    res.AppendChild(tag);
                }

                #endregion CITIZEN
                tag = GetCitStatus12mss(connection, doc, dr.GetInt32(4));
                res.AppendChild(tag);
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetRegion12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("REGION");
            String sql = "SELECT RCHI.DUE, RCHI.LAYER, RCHI.WEIGHT, RCHI.IS_NODE, RCHI.CLASSIF_NAME, RCHI.DELETED, ";
            sql += "RCHI.NOTE, RCHI.COD_OKATO, RCHI.CODE, RPAR.ISN_NODE, RPAR.LAYER, RPAR.WEIGHT, RPAR.CLASSIF_NAME, ";
            sql += "RPAR.DELETED, RPAR.NOTE, RPAR.CODE, RPAR.COD_OKATO FROM REGION_CL AS RCHI INNER JOIN ";
            sql += "REGION_CL AS RPAR ON RCHI.ISN_HIGH_NODE = RPAR.ISN_NODE WHERE RCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(1).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("COD_OKATO");
                    tag.InnerText = dr.GetString(7);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("CODE");
                    tag.InnerText = dr.GetInt16(8).ToString();
                    result.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("REGION");
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(12);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(13).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(14);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("CODE");
                    tag.InnerText = dr.GetInt16(15).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("COD_OKATO");
                    tag.InnerText = dr.GetString(16);
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetCitStatus12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode res = doc.CreateElement("CITSTATUSS");
            String sql = "SELECT CS.ISN_CIT_STAT, STCHI.ISN_NODE, STCHI.DUE, STCHI.LAYER, STCHI.WEIGHT, ";
            sql += "STCHI.IS_NODE, STCHI.CLASSIF_NAME, STCHI.DELETED, STCHI.NOTE, STPAR.ISN_NODE, STPAR.LAYER, ";
            sql += "STPAR.WEIGHT, STPAR.IS_NODE, STPAR.CLASSIF_NAME, STPAR.DELETED FROM ";
            sql += "CITIZEN_STATUS AS CS INNER JOIN CITSTATUS_CL AS STCHI ON CS.ISN_STATUS = STCHI.ISN_NODE INNER JOIN ";
            sql += "CITSTATUS_CL AS STPAR ON STCHI.ISN_HIGH_NODE = STPAR.ISN_NODE WHERE CS.ISN_CITIZEN=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode result = doc.CreateElement("CITSTATUS");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("DUE");
                    tag.InnerText = dr.GetString(2);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(12).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(13);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(14).ToString();
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
                res.AppendChild(result);
            }
            dr.Close();
            return res;
        }
        private static XmlNode GetWho12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("WHOS");
            String sql = "SELECT ISN_DOC_WHO, DUE_PERSON, ORDERNUM FROM DOC_WHO ";
            sql += "WHERE ISN_DOC=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("DOC_WHOS");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(1), "DUE_PERSON");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(2).ToString();
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetSign12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SIGNS");
            String sql = "SELECT ISN_DOC_SIGN, DUE_PERSON, ORDERNUM, SIGN_DATE FROM DOC_SIGN ";
            sql += "WHERE ISN_DOC=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("DOC_SIGN");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(1), "DUE_PERSON");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("SIGN_DATE");
                    tag.InnerText = dr.GetValue(3).ToString();
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetVisa12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("VISAS");
            String sql = "SELECT ISN_REF_VISA, DUE_PERSON, ORDERNUM, VISA_DATE, NOTE FROM REF_VISA ";
            sql += "WHERE ISN_DOC_OUT=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_VISA");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(1), "DUE_PERSON");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("VISA_DATE");
                    tag.InnerText = dr.GetValue(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetOrganiz12mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT OCHI.DUE, OCHI.ISN_NODE, OCHI.ISN_HIGH_NODE, OCHI.LAYER, OCHI.IS_NODE, OCHI.WEIGHT, OCHI.CLASSIF_NAME, OCHI.DELETED, OCHI.FULLNAME, "
                       + "OCHI.ZIPCODE, OCHI.CITY, OCHI.ADDRESS, OCHI.NOTE, OCHI.OKPO, OCHI.INN, OCHI.ISN_REGION, OCHI.OKONH, OCHI.LAW_ADRESS, "
                       + "OPAR.CLASSIF_NAME FROM ORGANIZ_CL AS OCHI INNER JOIN ORGANIZ_CL AS OPAR ON OCHI.ISN_HIGH_NODE = OPAR.ISN_NODE "
                       + "WHERE OCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(4).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("FULLNAME");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CITY");
                    tag.InnerText = dr.GetString(10);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("ADDRESS");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("OKPO");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("INN");
                    tag.InnerText = dr.GetString(13);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("ISN_REGION");
                    tag.InnerText = dr.GetInt32(14).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("OKONH");
                    tag.InnerText = dr.GetString(15);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("LAW_ADRESS");
                    tag.InnerText = dr.GetString(16);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("PARENT");
                    tag.InnerText = dr.GetString(17);
                    result.AppendChild(tag);
                }
            }

            dr.Close();
            return result;
        }
        private static XmlNode GetOrganiz12mss(OleDbConnection connection, XmlDocument doc, String due, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT OCHI.DUE, OCHI.ISN_NODE, OCHI.ISN_HIGH_NODE, OCHI.LAYER, OCHI.IS_NODE, OCHI.WEIGHT, OCHI.CLASSIF_NAME, OCHI.DELETED, OCHI.FULLNAME, "
                       + "OCHI.ZIPCODE, OCHI.CITY, OCHI.ADDRESS, OCHI.NOTE, OCHI.OKPO, OCHI.INN, OCHI.ISN_REGION, OCHI.OKONH, OCHI.LAW_ADRESS, "
                       + "OPAR.CLASSIF_NAME FROM ORGANIZ_CL AS OCHI INNER JOIN ORGANIZ_CL AS OPAR ON OCHI.ISN_HIGH_NODE = OPAR.ISN_NODE WHERE OCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt16(4).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("FULLNAME");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CITY");
                    tag.InnerText = dr.GetString(10);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("ADDRESS");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("OKPO");
                    tag.InnerText = dr.GetString(13);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("INN");
                    tag.InnerText = dr.GetString(14);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("ISN_REGION");
                    tag.InnerText = dr.GetInt32(15).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("OKONH");
                    tag.InnerText = dr.GetString(16);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("LAW_ADRESS");
                    tag.InnerText = dr.GetString(17);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag = doc.CreateElement("PARENT");
                    tag.InnerText = dr.GetString(18);
                    result.AppendChild(tag);
                }
            }

            dr.Close();
            return result;
        }
        private static XmlNode GetSoisp12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SOISPS");
            String sql = "SELECT ISN_REF_SOISP, DUE_ORGANIZ, ISN_CONTACT, SOISP_NUM, SOISP_DATE, SOISP_PERSON, ORDERNUM FROM REF_SOISP ";
            sql += "WHERE ISN_DOC_OUT=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_SOISP");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetOrganiz12mss(connection, doc, dr.GetString(1), "ORGANIZ");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_CONTACT");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("SOISP_NUM");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("SOISP_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag =  GetDepartment12mss(connection,doc,dr.GetString(5),"SOISP_PERSON");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(6).ToString();
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetLink12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("LINKS");
            String sql = "SELECT RL.ISN_REF_LINK, RL.LINKED_NUM, RL.URL_STR, RL.ORDERNUM, RL.REG_COUNT, ";
            sql += "LNK.ISN_LCLASSIF, LNK.CLASSIF_NAME, LNK.LINK_TYPE, LNK.LINK_DIR, LNK.DELETED, LNK.LINK_INDEX, LNK.NOTE, PLNK.ISN_LCLASSIF, ";
            sql += "PLNK.CLASSIF_NAME, PLNK.LINK_TYPE, PLNK.LINK_INDEX, RC.ISN_DOC, RC.DUE_DOCGROUP, RC.FREE_NUM, RC.DOC_DATE ";
            sql += "FROM REF_LINK AS RL INNER JOIN LINK_CL AS LNK ON RL.ISN_CLLINK = LNK.ISN_LCLASSIF INNER JOIN ";
            sql += "DOC_RC AS RC ON RL.ISN_LINKED_DOC = RC.ISN_DOC INNER JOIN LINK_CL AS PLNK ON LNK.ISN_PARE_LINK = PLNK.ISN_LCLASSIF ";
            sql += "WHERE RL.ISN_REF_DOC = " + isn.ToString() + " ORDER BY RL.ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("LINK");
                XmlAttribute attr = doc.CreateAttribute("isn");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("LINKED_NUM");
                    tag.InnerText = dr.GetString(1);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("URL_STR");
                    tag.InnerText = dr.GetString(2);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("REG_COUNT");
                    tag.InnerText = dr.GetInt16(4).ToString();
                    res.AppendChild(tag);
                }
                XmlNode lnk = doc.CreateElement("LNK");
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("ISN_LCLASSIF");
                    tag.InnerText = dr.GetInt32(5).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("LINK_TYPE");
                    tag.InnerText = dr.GetInt16(7).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("LINK_DIR");
                    tag.InnerText = dr.GetInt16(8).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt16(9).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("LINK_INDEX");
                    tag.InnerText = dr.GetString(10);
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(11);
                    lnk.AppendChild(tag);
                }
                XmlNode plnk = doc.CreateElement("PLNK");
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("ISN_LCLASSIF");
                    tag.InnerText = dr.GetInt32(12).ToString();
                    plnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(13);
                    plnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("LINK_TYPE");
                    tag.InnerText = dr.GetInt16(14).ToString();
                    plnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("LINK_INDEX");
                    tag.InnerText = dr.GetString(15);
                    plnk.AppendChild(tag);
                }
                lnk.AppendChild(plnk);
                res.AppendChild(lnk);
                XmlNode rc = doc.CreateElement("RC");
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("ISN_DOC");
                    tag.InnerText = dr.GetInt32(16).ToString();
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("DUE_DOCGROUP");
                    tag.InnerText = dr.GetString(17);
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag = doc.CreateElement("FREE_NUM");
                    tag.InnerText = dr.GetString(18);
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(19))
                {
                    tag = doc.CreateElement("DOC_DATE");
                    tag.InnerText = dr.GetValue(19).ToString();
                    rc.AppendChild(tag);
                }
                res.AppendChild(rc);
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetJournal12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("JOURNAL");
            String sql = "SELECT ISN_JOURNAL,KIND_DOC,DUE_CORRESP,ISN_CABINET,ISN_NOMENC,DUE_ADRESAT,SEND_DATE,NOTES,ORIGINAL_NUM, "
                       + "COPY_NAME,ORIG_FLAG,OWNER_FLAG,REC_TYPE FROM JOURNAL "
                       + "WHERE ISN_DOC = " + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("ENTRY");
                XmlAttribute attr = doc.CreateAttribute("ISN_JOURNAL");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("KIND_DOC");
                    tag.InnerText = dr.GetInt16(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(2), "DUE_CORRESP");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("ISN_CABINET");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("ISN_NOMENC");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetDepartment12mss(connection, doc, dr.GetString(5), "DUE_ADRESAT");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("SEND_DATE");
                    tag.InnerText = dr.GetValue(6).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("NOTES");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("ORIGINAL_NUM");
                    tag.InnerText = dr.GetInt32(8).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("COPY_NAME");
                    tag.InnerText = dr.GetString(9);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("ORIG_FLAG");
                    tag.InnerText = dr.GetInt16(10).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("OWNER_FLAG");
                    tag.InnerText = dr.GetInt16(11).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("REC_TYPE");
                    tag.InnerText = dr.GetInt16(12).ToString();
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetSend12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SENDS");
            String sql = "SELECT ISN_REF_SEND, ISN_CITIZEN, DUE_DEP, ISN_REESTR, SEND_PERSON, ORDERNUM"
                       + ", ISN_DELIVERY, SEND_DATE, ISN_SENDUSER, CONSISTS, ISN_CONTACT,DUE_ORGANIZ"
                       + ", FLAG_ORIGINAL, NOTE, SENDING_TYPE, REG_N, REG_DATE, ANSWER, ANSWER_DATE"
                       + " FROM REF_SEND WHERE ISN_REF_DOC ="
                       + isn.ToString() + " ORDER BY ORDERNUM ";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("SEND");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ISN_CITIZEN");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("DUE_DEP");
                    tag.InnerText = dr.GetString(2);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("ISN_REESTR");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("SEND_PERSON");
                    tag.InnerText = dr.GetString(4);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(5).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = GetDelivery12mss(connection, doc, dr.GetInt32(6));
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("SEND_DATE");
                    tag.InnerText = dr.GetValue(7).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("ISN_SENDUSER");
                    tag.InnerText = dr.GetInt32(8).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("CONSISTS");
                    tag.InnerText = dr.GetString(9);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("ISN_CONTACT");
                    tag.InnerText = dr.GetInt32(10).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = GetOrganiz12mss(connection, doc, dr.GetString(11), "DUE_ORGANIZ");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("FLAG_ORIGINAL");
                    tag.InnerText = dr.GetInt16(12).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("SENDING_TYPE");
                    tag.InnerText = dr.GetInt16(14).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("REG_N");
                    tag.InnerText = dr.GetString(15);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("REG_DATE");
                    tag.InnerText = dr.GetValue(16).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("ANSWER");
                    tag.InnerText = dr.GetInt16(17).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag = doc.CreateElement("ANSWER_DATE");
                    tag.InnerText = dr.GetValue(18).ToString();
                    res.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetCorresp12mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("CORRESPS");
            String sql = "SELECT ISN_REF_CORRESP, DUE_ORGANIZ, ISN_CONTACT, CORRESP_KIND, ORDERNUM, CORRESP_DATE, CORRESP_NUM, CORRESP_SIGN, TEL_NUM"
                       + ", ANNOTAT, DOC_NAME, CONSISTS, ANNOTAT_1, NOTE, NOTE_1 FROM REF_CORRESP"
                       + " WHERE ISN_DOC_INP =" + isn.ToString() + " ORDER BY ORDERNUM ";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("CORRESP");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetOrganiz12mss(connection,doc,dr.GetString(1),"DUE_ORGANIZ");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_CONTACT");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("CORRESP_KIND");
                    tag.InnerText = dr.GetInt16(3).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt16(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("CORRESP_DATE");
                    tag.InnerText = dr.GetValue(5).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CORRESP_NUM");
                    tag.InnerText = dr.GetString(6);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("CORRESP_SIGN");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("TEL_NUM");
                    tag.InnerText = dr.GetString(8);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ANNOTAT");
                    tag.InnerText = dr.GetString(9);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("DOC_NAME");
                    tag.InnerText = dr.GetString(10);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("CONSISTS");
                    tag.InnerText = dr.GetString(11);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("ANNOTAT_1");
                    tag.InnerText = dr.GetString(12);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("NOTE_1");
                    tag.InnerText = dr.GetString(14);
                    res.AppendChild(tag);
                }

                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        #endregion 12 Версия Microsoft SQL Server

        #region 7.54 Версия Oracle
        public static Int32 CountRows754mss(OleDbConnection connection)
        {
            String sqlyear = "";
            if (year != 0) sqlyear = " WHERE (DOC_YEAR = " + year.ToString() + ") ";
            String sql = "select SUM(summa) FROM (SELECT count(*) as summa FROM RC_INP  " + sqlyear + "  union SELECT count(*) as summa FROM RC_OUT  "
                        + sqlyear + " ) as total";
            OleDbCommand cmdDocuments = new OleDbCommand(sql, connection);
            return (Int32)cmdDocuments.ExecuteScalar();
        }
        public static void Execute754mss(String Folder, OleDbConnection connection)
        {
            String sqlyear ="";
            if (year != 0) sqlyear = " WHERE (DOC_YEAR = " + year.ToString() + ") ";
            //CurrentMessage("Состояние выгрузки: Выгрузка документов");
            String sql = "SELECT KIND_DOC,ISN_DOC_INP as ISN_DOC,DUE_DOCGROUP,ORDER_NUM,FREE_NUM,DOC_DATE"
                        +",SECURLEVEL,CONSISTS,SPECIMEN,null as PLAN_DATE,null as FACT_DATE,ANNOTAT,NOTE"
                        +",ISN_DELIVERY,ISCOLLECTIVE,null as DUE_PERSON_EXE"
                        + ",DUE_PERSON_WHO as DUE_PERSON FROM RC_INP "+sqlyear+" union SELECT 3 as KIND_DOC,ISN_DOC_OUT as ISN_DOC,DUE_DOCGROUP"
                        + ",ORDER_NUM,FREE_NUM,DOC_DATE,SECURLEVEL,CONSISTS,SPECIMEN,null as PLAN_DATE,null as FACT_DATE"
                        +",ANNOTAT,NOTE,null as ISN_DELIVERY,null as ISCOLLECTIVE,DUE_PERSON_EXE"
                        + ",DUE_PERSON_SIGN as DUE_PERSON FROM RC_OUT  " + sqlyear + " order by ISN_DOC";
            OleDbCommand cmdDocuments = new OleDbCommand(sql, connection);
            OleDbDataReader drDocuments = cmdDocuments.ExecuteReader();
            CurrentRows(0);
            int i = 0;
            //StreamWriter _file = File.CreateText(Folder + "\\protocol.log");
            int CountThread = 0;
            while (drDocuments.Read())
            {
                System.Windows.Forms.Application.DoEvents();
                i++;
                CurrentRows(i);
                String s_kind = "", s_isn = "", s_ordernum = "", s_freenum = "", s_docdate = "",
                    s_consists = "", s_speciment = "", s_plandate = "", s_factdate = "",
                    s_annotat = "", s_note = "", s_iscollective = "", s_personexe = "",
                    s_21 ="", s_docgroup = "", s_securlevel = "", s_delivery = "";
                s_kind =drDocuments.GetInt32(0).ToString();
                s_isn = drDocuments.GetInt32(1).ToString();
                if (File.Exists(Folder + "\\" + s_isn + ".xml")) continue;
                if (!drDocuments.IsDBNull(2))
                {
                    s_docgroup = drDocuments.GetString(2);
                }
                if (!drDocuments.IsDBNull(3))
                {
                    s_ordernum = drDocuments.GetInt32(3).ToString();
                }
                if (!drDocuments.IsDBNull(4))
                {
                    s_freenum = drDocuments.GetString(4);
                }
                if (!drDocuments.IsDBNull(5))
                {                
                    s_docdate = drDocuments.GetValue(5).ToString();
                }
                if (!drDocuments.IsDBNull(6))
                {
                    s_securlevel = drDocuments.GetInt32(6).ToString();
                }
                if (!drDocuments.IsDBNull(7))
                {
                    s_consists = drDocuments.GetString(7);
                }
                if (!drDocuments.IsDBNull(8))
                {
                    s_speciment = drDocuments.GetString(8);
                }
                if (!drDocuments.IsDBNull(9))
                {
                    s_plandate = drDocuments.GetValue(9).ToString();
                }
                if (!drDocuments.IsDBNull(10))
                {
                    s_factdate = drDocuments.GetValue(10).ToString();
                }
                if (!drDocuments.IsDBNull(11))
                {
                    s_annotat = drDocuments.GetString(11);
                }
                if (!drDocuments.IsDBNull(12))
                {
                    s_note = drDocuments.GetString(12);
                }
                if (!drDocuments.IsDBNull(13))
                {
                    s_delivery = drDocuments.GetInt32(13).ToString();
                }
                if (!drDocuments.IsDBNull(14))
                {
                    s_iscollective = drDocuments.GetInt32(14).ToString();
                }
                if (!drDocuments.IsDBNull(15))
                {
                    s_personexe = drDocuments.GetString(15);
                }
                if (!drDocuments.IsDBNull(16))
                {
                    s_21 = drDocuments.GetString(16);
                }
                Thread potok = new Thread(delegate()
                {
                    CountThread++;
                    XmlDocument doc = new XmlDocument();
                    XmlNode root = doc.CreateElement("root", "");
                    XmlNode rc = doc.CreateElement("rc", "");
                    XmlAttribute kind = doc.CreateAttribute("kind");
                    kind.Value = s_kind;
                    rc.Attributes.Append(kind);
                    XmlAttribute isn = doc.CreateAttribute("isn");
                    isn.Value = s_isn;
                    rc.Attributes.Append(isn);
                    XmlNode tag;
                    try
                    {
                        #region RC-ATTR

                        if (s_docgroup!="")
                        {
                            tag = GetDocgroup754mss(connection, doc, s_docgroup);
                            rc.AppendChild(tag);
                        }

                        if (s_ordernum!="")
                        {
                            tag = doc.CreateElement("ORDER_NUM", "");
                            tag.InnerText = s_ordernum;
                            rc.AppendChild(tag);
                        }

                        if (s_freenum!="")
                        {
                            tag = doc.CreateElement("FREE_NUM", "");
                            tag.InnerText = s_freenum;
                            rc.AppendChild(tag);
                        }

                        if (s_docdate!="")
                        {
                            tag = doc.CreateElement("DOC_DATE", "");
                            tag.InnerText = s_docdate;
                            rc.AppendChild(tag);
                        }

                        if (s_securlevel!="")
                        {
                            tag = GetSecurlevel754mss(connection, doc, Int32.Parse(s_securlevel));
                            rc.AppendChild(tag);
                        }

                        if (s_consists!="")
                        {
                            tag = doc.CreateElement("CONSISTS", "");
                            tag.InnerText = s_consists;
                            rc.AppendChild(tag);
                        }

                        if (s_speciment!="")
                        {
                            tag = doc.CreateElement("SPECIMEN", "");
                            tag.InnerText = s_speciment;
                            rc.AppendChild(tag);
                        }

                        if (s_plandate!="")
                        {
                            tag = doc.CreateElement("PLAN_DATE", "");
                            tag.InnerText = s_plandate;
                            rc.AppendChild(tag);
                        }

                        if (s_factdate!="")
                        {
                            tag = doc.CreateElement("FACT_DATE", "");
                            tag.InnerText = s_factdate;
                            rc.AppendChild(tag);
                        }

                        //if (!drDocuments.IsDBNull(12))
                        //{
                        //    tag = doc.CreateElement("CONTROL_STATE", "");
                        //    tag.InnerText = drDocuments.GetInt32(12).ToString();
                        //    rc.AppendChild(tag);
                        //}

                        if (s_annotat!="")
                        {
                            tag = doc.CreateElement("ANNOTAT", "");
                            tag.InnerText = s_annotat;
                            rc.AppendChild(tag);
                        }

                        if (s_note!="")
                        {
                            tag = doc.CreateElement("NOTE", "");
                            tag.InnerText = s_note;
                            rc.AppendChild(tag);
                        }

                        if (s_delivery!="")
                        {
                            tag = GetDelivery754mss(connection, doc, Int32.Parse(s_delivery));
                            rc.AppendChild(tag);
                        }

                        if (s_iscollective!="")
                        {
                            tag = doc.CreateElement("ISCOLLECTIVE", "");
                            tag.InnerText = s_iscollective;
                            rc.AppendChild(tag);
                        }

                        //if (!drDocuments.IsDBNull(17))
                        //{
                        //    tag = doc.CreateElement("ANONIM", "");
                        //    tag.InnerText = drDocuments.GetInt32(17).ToString();
                        //    rc.AppendChild(tag);
                        //}

                        if (s_personexe!="")
                        {
                            tag = GetDepartment754mss(connection, doc, s_personexe, "PERSON_EXE");
                            rc.AppendChild(tag);
                        }
                        //if (!drDocuments.IsDBNull(19))
                        //{
                        //    tag = GetUser754mss(connection, doc, drDocuments.GetInt32(19), "INS_WHO");
                        //    rc.AppendChild(tag);
                        //}
                        //if (!drDocuments.IsDBNull(20))
                        //{
                        //    tag = doc.CreateElement("INS_DATE");
                        //    tag.InnerText = drDocuments.GetValue(20).ToString();
                        //    rc.AppendChild(tag);
                        //}
                        #endregion RC-ATTR
                        tag = GetResolution754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetRubric754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);

                        tag = GetLetter754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        if ((s_21 != "") && (s_kind != "3"))
                        {
                            tag = GetWho754mss(connection, doc, s_21);
                            rc.AppendChild(tag);
                        }
                        if ((s_21!="") && (s_kind == "3"))
                        {
                            tag = GetSign754mss(connection, doc, s_21);
                            rc.AppendChild(tag);
                        }
                        tag = GetVisa754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetSoisp754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetLink754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetSend754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetCorresp754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        tag = GetJournal754mss(connection, doc, Int32.Parse(s_isn));
                        rc.AppendChild(tag);
                        root.AppendChild(rc);
                        doc.AppendChild(root);
                        doc.Save(Folder + "\\" + s_isn + ".xml");
                        if ((localdb!=null) && (localdb.Connection.State & ConnectionState.Open) == ConnectionState.Open)
                        {
                            Data _data = new Data("RC", DateTime.Now, Int32.Parse(s_isn), string.Empty);
                            _data.SaveDB(localdb);
                            //_file.WriteLine(_data);
                        }
                    }
                    catch (Exception ex)
                    {
                        File.WriteAllText(Folder + "\\" + s_isn + ".err", ex.Message);
                    }
                    CountThread--;
                });
                potok.Start();
                if (CountThread >= MaxThread) potok.Join();
            }
            drDocuments.Close();
            //_file.Close();
        }
        private static XmlNode GetDepartment754mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT DCHI.DUE, DCHI.ISN_NODE, DCHI.ISN_HIGH_NODE, DCHI.IS_NODE, "
                        + "DCHI.CLASSIF_NAME, DCHI.SURNAME, DCHI.DUTY, "
                        + "DCHI.NOTE, DPAR.CLASSIF_NAME AS PARENTNAME FROM DEPARTMENT AS DCHI "
                        + "INNER JOIN DEPARTMENT AS DPAR ON DCHI.ISN_HIGH_NODE = DPAR.ISN_NODE WHERE DCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("SURNAME");
                    tag.InnerText = dr.GetString(5);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("DUTY");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(7);
                    result.AppendChild(tag);
                }

                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(8);
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDepartment754mss(OleDbConnection connection, XmlDocument doc, String due, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT DCHI.DUE, DCHI.ISN_NODE, DCHI.ISN_HIGH_NODE, DCHI.IS_NODE, "
                        + "DCHI.CLASSIF_NAME, DCHI.SURNAME, DCHI.DUTY, "
                        + "DCHI.NOTE, DPAR.CLASSIF_NAME AS PARENTNAME FROM DEPARTMENT AS DCHI "
                        +"INNER JOIN DEPARTMENT AS DPAR ON DCHI.ISN_HIGH_NODE = DPAR.ISN_NODE WHERE DCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("SURNAME");
                    tag.InnerText = dr.GetString(5);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("DUTY");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(7);
                    result.AppendChild(tag);
                }

                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(8);
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDocgroup754mss(OleDbConnection connection, XmlDocument doc, String due)
        {
            XmlNode result = doc.CreateElement("DOCGROUP");
            String sql = "SELECT DGCHI.DUE, DGCHI.ISN_NODE, DGCHI.ISN_HIGH_NODE, DGCHI.IS_NODE, DGCHI.CLASSIF_NAME, ";
            sql += "DGCHI.PROTECTED, DGCHI.DELETED, DGCHI.RC_TYPE, DGCHI.DOCGROUP_INDEX, DGCHI.NOTE, DGPAR.CLASSIF_NAME, ";
            sql += "DGPAR.RC_TYPE FROM DOCGROUP_CL AS DGCHI INNER JOIN DOCGROUP_CL AS DGPAR ON DGCHI.ISN_HIGH_NODE = DGPAR.ISN_NODE ";
            sql += "WHERE DGCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("IS_NODE");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("PROTECTED");
                //    tag.InnerText = dr.GetInt32(5).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(6))
                //{
                //    tag = doc.CreateElement("DELETED");
                //    tag.InnerText = dr.GetInt32(6).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("RC_TYPE");
                    tag.InnerText = dr.GetInt32(7).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("DOCGROUP_INDEX");
                //    tag.InnerText = dr.GetString(8);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(9))
                //{
                //    tag = doc.CreateElement("NOTE");
                //    tag.InnerText = dr.GetString(9);
                //    result.AppendChild(tag);
                //}
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(10);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("RC_TYPE");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    parent.AppendChild(tag);
                }
                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetSecurlevel754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SECURLEVEL");
            String sql = "SELECT SECURLEVEL, GRIF_NAME FROM SECURITY_CL ";
            sql += "WHERE SECURLEVEL=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(1))
                {
                    result.InnerText = dr.GetString(1);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetDelivery754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("DELIVERY");
            String sql = "SELECT  ISN_LCLASSIF, CLASSIF_NAME FROM DELIVERY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(1))
                {
                    result.InnerText = dr.GetString(1);
                }
            }
            dr.Close();
            return result;
        }

        private static XmlNode GetJournal754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode res = doc.CreateElement("JOURNALS");
            String sql = "SELECT SEND_DATE, DUE_ADRESAT, NOTES, ORIG_FLAG, ORIGINAL_NUM, OWNER_FLAG FROM JOURNAL ";
            sql += "WHERE isn_doc=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode result = doc.CreateElement("JOURNAL");
                if (!dr.IsDBNull(0))
                {
                    XmlNode tag = doc.CreateElement("SEND_DATE");
                    tag.InnerText = dr.GetValue(0).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(1))
                {
                    XmlNode tag = GetDepartment754mss(connection, doc, dr.GetString(1), "DUE_ADRESAT");
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(2))
                {
                    XmlNode tag = doc.CreateElement("NOTES");
                    tag.InnerText = dr.GetString(2);
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(3))
                {
                    XmlNode tag = doc.CreateElement("ORIG_FLAG");
                    tag.InnerText = dr.GetValue(3).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(4))
                {
                    XmlNode tag = doc.CreateElement("ORIGINAL_NUM");
                    tag.InnerText = dr.GetValue(4).ToString();
                    result.AppendChild(tag);
                }

                if (!dr.IsDBNull(5))
                {
                    XmlNode tag = doc.CreateElement("OWNER_FLAG");
                    tag.InnerText = dr.GetValue(5).ToString();
                    result.AppendChild(tag);
                }

                res.AppendChild(result);
            }
            dr.Close();
            return res;
        }

        private static XmlNode GetResolution754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("RESOLUTIONS");
            String sql = "SELECT ISN_RESOLUTION, 1 as KIND_RESOLUTION, null as ISN_PARENT_RESOLUTION"
                        +",1 as LAYER, null as WEIGHT, null as ITEM_NUMBER, DUE, RESOLUTION_TEXT"
                        +",null as ISN_CATEGORY, 0 as CONFIDENTIONAL,RESOLUTION_DATE,SEND_DATE"
                        +",PLAN_DATE,INTERIM_DATE,FACT_DATE,DELTA_DATE,null as DUE_CONTROLLER"
                        +",null as CONTROL_STATE,null as ISN_STATUS_EXEC,SUMMARY,0 as CASCADE_CONTROL"
                        +",RESUME,LEFT_REZOLUTION,null as NOTE,null as INS_WHO,null as INS_DATE FROM "
                        +"RESOLUTION WHERE ISN_REF_DOC=" + isn.ToString()
                        +" ORDER BY LAYER, WEIGHT, RESOLUTION_DATE";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("RESOLUTION");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                //if (!dr.IsDBNull(1))
                //{
                //    tag = doc.CreateElement("KIND_RESOLUTION");
                //    tag.InnerText = dr.GetInt32(1).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(2))
                //{
                //    tag = doc.CreateElement("ISN_PARENT_RESOLUTION");
                //    tag.InnerText = dr.GetInt32(2).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("LAYER");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(4))
                //{
                //    tag = doc.CreateElement("WEIGHT");
                //    tag.InnerText = dr.GetInt32(4).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("ITEM_NUMBER");
                //    tag.InnerText = dr.GetString(5);
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(6))
                {
                    tag = GetDepartment754mss(connection, doc, dr.GetString(6), "DUE");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("RESOLUTION_TEXT");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(8))
                //{
                //    tag = GetResolutionCategory754mss(connection, doc, dr.GetInt32(8));
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(9))
                //{
                //    tag = doc.CreateElement("CONFIDENTIONAL");
                //    tag.InnerText = dr.GetInt32(9).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("RESOLUTION_DATE");
                    tag.InnerText = dr.GetValue(10).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("SEND_DATE");
                    tag.InnerText = dr.GetValue(11).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("PLAN_DATE");
                    tag.InnerText = dr.GetValue(12).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("INTERIM_DATE");
                    tag.InnerText = dr.GetValue(13).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("FACT_DATE");
                    tag.InnerText = dr.GetValue(14).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("DELTA_DATE");
                    tag.InnerText = dr.GetInt32(15).ToString();
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(16))
                //{
                //    tag = GetDepartment754mss(connection, doc, dr.GetString(16), "DUE_CONTROLLER");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(17))
                //{
                //    tag = doc.CreateElement("CONTROL_STATE");
                //    tag.InnerText = dr.GetInt32(17).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(18))
                //{
                //    tag = GetResolutionStatus754mss(connection, doc, dr.GetInt32(18));
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(19))
                {
                    tag = doc.CreateElement("SUMMARY");
                    tag.InnerText = dr.GetString(19);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(20))
                //{
                //    tag = doc.CreateElement("CASCADE_CONTROL");
                //    tag.InnerText = dr.GetInt32(20).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(21))
                {
                    tag = doc.CreateElement("RESUME");
                    tag.InnerText = dr.GetString(21);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(22))
                //{
                //    tag = doc.CreateElement("LEFT_REZOLUTION");
                //    tag.InnerText = dr.GetInt32(22).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(23))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(23);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(24))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(24), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(25))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(25).ToString();
                //    res.AppendChild(tag);
                //}
                tag = GetReply754mss(connection, doc, dr.GetInt32(0));
                res.AppendChild(tag);
                result.AppendChild(res);
            }

            return result;
        }
        private static XmlNode GetUser754mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT null as DUE_DEP, SURNAME_PATRON, CLASSIF_NAME, NOTE, ORACLE_ID FROM USER_CL "
                        +"WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(0))
                {
                    tag = GetDepartment754mss(connection, doc, dr.GetString(0), "DUE_DEP");
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("SURNAME_PATRON");
                    tag.InnerText = dr.GetString(1);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(2);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(3);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("ORACLE_ID");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetResolutionCategory754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("ISN_CATEGORY");
            String sql = "SELECT CLASSIF_NAME FROM RESOLUTION_CATEGORY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetResolutionStatus754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("STATUS_EXEC");
            String sql = "SELECT CLASSIF_NAME FROM STATUS_EXEC_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetReplyStatus754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("STATUS_REPLY");
            String sql = "SELECT CLASSIF_NAME FROM STATUS_REPLY_CL ";
            sql += "WHERE ISN_LCLASSIF=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                if (!dr.IsDBNull(0))
                {
                    result.InnerText = dr.GetString(0);
                }
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetReply754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("REPLYS");
            String sql = "SELECT  ISN_REPLY, DUE, MAIN_FLAG, REPLY_TEXT,REPLY_DATE"
                        +",null as  ISN_STATUS_REPLY, null as INS_DATE,null as INS_WHO"
                        +",null as UPD_DATE,null as UPD_WHO FROM REPLY WHERE ISN_RESOLUTION=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            XmlNode res;
            while (dr.Read())
            {
                res = doc.CreateElement("REPLY");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment754mss(connection, doc, dr.GetString(1), "DUE");
                    res.AppendChild(tag);
                }
                tag = doc.CreateElement("ISN_RESOLUTION");
                tag.InnerText = isn.ToString();
                res.AppendChild(tag);
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("MAIN_FLAG");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("REPLY_TEXT");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("REPLY_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetReplyStatus754mss(connection, doc, dr.GetInt32(5));
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(6))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(6).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(7))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(7), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(8).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(9))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(9), "UPD_WHO");
                //    res.AppendChild(tag);
                //}
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetRubric754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode res = doc.CreateElement("REF_RUBRICS");
            String sql = "SELECT RF.ISN_REF_RUBRIC, RF.ORDERNUM, RF.INS_DATE, RF.INS_WHO, RF.UPD_DATE, RF.UPD_WHO"
                        +",RCHI.DUE, RCHI.ISN_NODE, RCHI.LAYER, RCHI.IS_NODE, RCHI.WEIGHT, RCHI.CLASSIF_NAME, RCHI.RUBRIC_CODE"
                        +",RCHI.NOTE, RPAR.ISN_NODE, RPAR.CLASSIF_NAME, RPAR.RUBRIC_CODE, RPAR.NOTE FROM RUBRIC_CL AS RCHI "
                        +"INNER JOIN RUBRIC_CL AS RPAR ON RCHI.ISN_HIGH_NODE = RPAR.ISN_NODE INNER JOIN REF_RUBRIC AS "
                        +"RF ON RCHI.DUE = RF.DUE_RUBRIC WHERE RF.ISN_REF_DOC=" + isn.ToString() + " ORDER BY RF.ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode result = doc.CreateElement("REF_RUBRIC");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("INS_DATE");
                    tag.InnerText = dr.GetValue(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(3), "INS_WHO");
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("UPD_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(5), "UPD_WHO");
                    result.AppendChild(tag);
                }
                XmlNode rubric = doc.CreateElement("RUBRIC");
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(6);
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(7).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(8).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(10).ToString();
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(11);
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("RUBRIC_CODE");
                    tag.InnerText = dr.GetString(12);
                    rubric.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    rubric.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(14).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(15);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("RUBRIC_CODE");
                    tag.InnerText = dr.GetString(16);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(17);
                    parent.AppendChild(tag);
                }
                rubric.AppendChild(parent);
                result.AppendChild(rubric);
                res.AppendChild(result);
            }
            dr.Close();
            return res;
        }
        private static XmlNode GetAR754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("ARs");
            String sql = "SELECT * FROM AR_RC_VALUE ";
            sql += "WHERE ISN_RC=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            XmlNode tag;
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("ARs");
                for (int i = 0; i < dr.FieldCount - 1; i++)
                {
                    if (!dr.IsDBNull(i))
                    {
                        tag = doc.CreateElement(dr.GetName(i));
                        tag.InnerText = dr.GetValue(i).ToString();
                        res.AppendChild(tag);
                    }
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetLetter754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            String sql = "SELECT RFL.ISN_REF_LETTER, RFL.NEEDANSWER, RFL.ORDERNUM,RFL.TEL_NUM, RFL.INS_DATE"
                        +", RFL.INS_WHO, RFL.UPD_DATE, RFL.UPD_WHO,CIT.ISN_CITIZEN, CIT.CITIZEN_SURNAME"
                        +", CIT.CITIZEN_CITY, CIT.ZIPCODE, CIT.CITIZEN_ADDR,null,CIT.DELETED"
                        +", null , null, null, null, null, null, null"
                        +",null, null FROM REF_LETTER AS RFL INNER JOIN CITIZEN AS CIT ON "
                        +"RFL.ISN_CITIZEN = CIT.ISN_CITIZEN WHERE RFL.ISN_DOC_INP=" + isn.ToString() + " ORDER BY RFL.ORDERNUM";
            XmlNode result = doc.CreateElement("REF_LETTERS");
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_LETTER");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.InnerText = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                #region REF_LETTER;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("NEEDANSWER");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("TEL_NUM");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("INS_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(5), "INS_WHO");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("UPD_DATE");
                    tag.InnerText = dr.GetValue(6).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(7), "UPD_WHO");
                    res.AppendChild(tag);
                }
                #endregion REF_LETTER
                #region CITIZEN
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("ISN_CITIZEN");
                    tag.InnerText = dr.GetInt32(8).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("CITIZEN_SURNAME");
                    tag.InnerText = dr.GetString(9);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CITIZEN_CITY");
                    tag.InnerText = dr.GetString(10);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(11);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("CITIZEN_ADDR");
                    tag.InnerText = dr.GetString(12);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = GetRegion754mss(connection, doc, dr.GetInt32(13));
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt32(14).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("SEX");
                    tag.InnerText = dr.GetInt32(15).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("PHONE");
                    tag.InnerText = dr.GetString(16);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("INN");
                    tag.InnerText = dr.GetString(17);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag = doc.CreateElement("E_MAIL");
                    tag.InnerText = dr.GetString(18);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(19))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(19);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(20))
                {
                    tag = doc.CreateElement("INS_DATE");
                    tag.InnerText = dr.GetValue(20).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(21))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(21), "INS_WHO");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(22))
                {
                    tag = doc.CreateElement("UPD_DATE");
                    tag.InnerText = dr.GetValue(22).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(23))
                {
                    tag = GetUser754mss(connection, doc, dr.GetInt32(23), "UPD_WHO");
                    res.AppendChild(tag);
                }

                #endregion CITIZEN
                //tag = GetCitStatus754mss(connection, doc, dr.GetInt32(8));
                //res.AppendChild(tag);
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }

        private static XmlNode GetRegion754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("REGION");
            String sql = "SELECT RCHI.DUE, RCHI.LAYER, RCHI.WEIGHT, RCHI.IS_NODE, RCHI.CLASSIF_NAME, RCHI.DELETED, ";
            sql += "RCHI.NOTE, RCHI.COD_OKATO, RCHI.CODE, RPAR.ISN_NODE, RPAR.LAYER, RPAR.WEIGHT, RPAR.CLASSIF_NAME, ";
            sql += "RPAR.DELETED, RPAR.NOTE, RPAR.CODE, RPAR.COD_OKATO FROM REGION_CL AS RCHI INNER JOIN ";
            sql += "REGION_CL AS RPAR ON RCHI.ISN_HIGH_NODE = RPAR.ISN_NODE WHERE RCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = isn.ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(4);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt32(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("COD_OKATO");
                    tag.InnerText = dr.GetString(7);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("CODE");
                    tag.InnerText = dr.GetInt32(8).ToString();
                    result.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("REGION");
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(10).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(12);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt32(13).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(14);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(15))
                {
                    tag = doc.CreateElement("CODE");
                    tag.InnerText = dr.GetInt32(15).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("COD_OKATO");
                    tag.InnerText = dr.GetString(16);
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetCitStatus754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode res = doc.CreateElement("CITSTATUSS");
            String sql = "SELECT CS.ISN_CIT_STAT, STCHI.ISN_NODE, STCHI.DUE, STCHI.LAYER, STCHI.WEIGHT, ";
            sql += "STCHI.IS_NODE, STCHI.CLASSIF_NAME, STCHI.DELETED, STCHI.NOTE, STPAR.ISN_NODE, STPAR.LAYER, ";
            sql += "STPAR.WEIGHT, STPAR.IS_NODE, STPAR.CLASSIF_NAME, STPAR.DELETED FROM ";
            sql += "CITIZEN_STATUS AS CS INNER JOIN CITSTATUS_CL AS STCHI ON CS.ISN_STATUS = STCHI.ISN_NODE INNER JOIN ";
            sql += "CITSTATUS_CL AS STPAR ON STCHI.ISN_HIGH_NODE = STPAR.ISN_NODE WHERE CS.ISN_CITIZEN=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode result = doc.CreateElement("CITSTATUS");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("DUE");
                    tag.InnerText = dr.GetString(2);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(5).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt32(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                XmlNode parent = doc.CreateElement("PARENT");
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ISN_NODE");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("LAYER");
                    tag.InnerText = dr.GetInt32(10).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("WEIGHT");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(12).ToString();
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(13);
                    parent.AppendChild(tag);
                }
                if (!dr.IsDBNull(14))
                {
                    tag = doc.CreateElement("DELETED");
                    tag.InnerText = dr.GetInt32(14).ToString();
                    parent.AppendChild(tag);
                }

                result.AppendChild(parent);
                res.AppendChild(result);
            }
            dr.Close();
            return res;
        }
        private static XmlNode GetWho754mss(OleDbConnection connection, XmlDocument doc, String DUE)
        {
            XmlNode result = doc.CreateElement("WHOS");
                XmlNode res = doc.CreateElement("DOC_WHOS");
                XmlNode tag = GetDepartment754mss(connection, doc, DUE, "DUE_PERSON");
                    res.AppendChild(tag);
                result.AppendChild(res);
            return result;
        }
        private static XmlNode GetSign754mss(OleDbConnection connection, XmlDocument doc, String DUE)
        {
            XmlNode result = doc.CreateElement("SIGNS");
                XmlNode res = doc.CreateElement("DOC_SIGN");
                XmlNode tag= GetDepartment754mss(connection, doc, DUE, "DUE_PERSON");
                    res.AppendChild(tag);
                result.AppendChild(res);
            return result;
        }
        private static XmlNode GetVisa754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("VISAS");
            String sql = "SELECT ISN_REF_VISA, DUE_PERSON, ORDERNUM, INS_DATE, INS_WHO, UPD_DATE, UPD_WHO, VISA_DATE, NOTE FROM REF_VISA ";
            sql += "WHERE ISN_DOC_OUT=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_VISA");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetDepartment754mss(connection, doc, dr.GetString(1), "DUE_PERSON");
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(2))
                //{
                //    tag = doc.CreateElement("ORDERNUM");
                //    tag.InnerText = dr.GetInt32(2).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(3).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(4))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(4), "INS_WHO");
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(5).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(6))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(6), "UPD_WHO");
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("VISA_DATE");
                    tag.InnerText = dr.GetValue(7).ToString();
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(8))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(8);
                    result.AppendChild(tag);
                }
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetOrganiz754mss(OleDbConnection connection, XmlDocument doc, Int32 isn, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT OCHI.DUE, OCHI.ISN_NODE, OCHI.ISN_HIGH_NODE, OCHI.LAYER, OCHI.IS_NODE, OCHI.WEIGHT, OCHI.CLASSIF_NAME, OCHI.DELETED, OCHI.FULLNAME, ";
            sql += "OCHI.ZIPCODE,null as CITY, OCHI.ADDRESS, OCHI.NOTE, OCHI.OKPO, OCHI.INN, OCHI.ISN_REGION, OCHI.OKONH, OCHI.LAW_ADRESS,null as INS_DATE, ";
            sql += "null as INS_WHO,null as UPD_DATE,null as UPD_WHO, OPAR.CLASSIF_NAME FROM ORGANIZ_CL AS OCHI INNER JOIN ORGANIZ_CL AS OPAR ON OCHI.ISN_HIGH_NODE = OPAR.ISN_NODE ";
            sql += "WHERE OCHI.ISN_NODE=" + isn.ToString();
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("LAYER");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("WEIGHT");
                //    tag.InnerText = dr.GetInt32(5).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(7))
                //{
                //    tag = doc.CreateElement("DELETED");
                //    tag.InnerText = dr.GetInt32(7).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("FULLNAME");
                //    tag.InnerText = dr.GetString(8);
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CITY");
                    tag.InnerText = dr.GetString(10);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("ADDRESS");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(12))
                //{
                //    tag = doc.CreateElement("OKPO");
                //    tag.InnerText = dr.GetString(12);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(13))
                //{
                //    tag = doc.CreateElement("INN");
                //    tag.InnerText = dr.GetString(13);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(14))
                //{
                //    tag = doc.CreateElement("ISN_REGION");
                //    tag.InnerText = dr.GetInt32(14).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(15))
                //{
                //    tag = doc.CreateElement("OKONH");
                //    tag.InnerText = dr.GetString(15);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(16))
                //{
                //    tag = doc.CreateElement("LAW_ADRESS");
                //    tag.InnerText = dr.GetString(16);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(17))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(17).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(18))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(18), "INS_WHO");
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(19))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(19).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(20))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(20), "UPD_WHO");
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(21))
                {
                    tag = doc.CreateElement("PARENT");
                    tag.InnerText = dr.GetString(21);
                    result.AppendChild(tag);
                }
            }

            dr.Close();
            return result;
        }
        private static XmlNode GetOrganiz754mss(OleDbConnection connection, XmlDocument doc, String due, String Name)
        {
            XmlNode result = doc.CreateElement(Name);
            String sql = "SELECT OCHI.DUE,OCHI.ISN_NODE,OCHI.ISN_HIGH_NODE,OCHI.LAYER,OCHI.IS_NODE,OCHI.WEIGHT,OCHI.CLASSIF_NAME,OCHI.DELETED"
                        +",OCHI.FULLNAME,OCHI.ZIPCODE,null as CITY,OCHI.ADDRESS,OCHI.NOTE,OCHI.OKPO,OCHI.INN,OCHI.ISN_REGION,OCHI.OKONH"
                        +",OCHI.LAW_ADRESS,null as INS_DATE,null as INS_WHO,null as UPD_DATE,null as UPD_WHO,OPAR.CLASSIF_NAME FROM "
                        +"ORGANIZ_CL AS OCHI INNER JOIN ORGANIZ_CL AS OPAR ON OCHI.ISN_HIGH_NODE = OPAR.ISN_NODE WHERE OCHI.DUE='" + due + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(1).ToString();
                result.Attributes.Append(attr);
                attr = doc.CreateAttribute("DUE");
                attr.Value = dr.GetString(0);
                result.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("ISN_HIGH_NODE");
                    tag.InnerText = dr.GetInt32(2).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("LAYER");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("IS_NODE");
                    tag.InnerText = dr.GetInt32(4).ToString();
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("WEIGHT");
                //    tag.InnerText = dr.GetInt32(5).ToString();
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(6);
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(7))
                //{
                //    tag = doc.CreateElement("DELETED");
                //    tag.InnerText = dr.GetInt32(7).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("FULLNAME");
                //    tag.InnerText = dr.GetString(8);
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ZIPCODE");
                    tag.InnerText = dr.GetString(9);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CITY");
                    tag.InnerText = dr.GetString(10);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("ADDRESS");
                    tag.InnerText = dr.GetString(11);
                    result.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(12);
                    result.AppendChild(tag);
                }
                //if (!dr.IsDBNull(13))
                //{
                //    tag = doc.CreateElement("OKPO");
                //    tag.InnerText = dr.GetString(13);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(14))
                //{
                //    tag = doc.CreateElement("INN");
                //    tag.InnerText = dr.GetString(14);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(15))
                //{
                //    tag = doc.CreateElement("ISN_REGION");
                //    tag.InnerText = dr.GetInt32(15).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(16))
                //{
                //    tag = doc.CreateElement("OKONH");
                //    tag.InnerText = dr.GetString(16);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(17))
                //{
                //    tag = doc.CreateElement("LAW_ADRESS");
                //    tag.InnerText = dr.GetString(17);
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(18))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(18).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(19))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(19), "INS_WHO");
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(20))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(20).ToString();
                //    result.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(21))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(21), "UPD_WHO");
                //    result.AppendChild(tag);
                //}
                if (!dr.IsDBNull(22))
                {
                    tag = doc.CreateElement("PARENT");
                    tag.InnerText = dr.GetString(22);
                    result.AppendChild(tag);
                }
            }

            dr.Close();
            return result;
        }
        private static XmlNode GetSoisp754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SOISPS");
            String sql = "SELECT ISN_REF_SOISP, DUE_ORGANIZ,null as ISN_CONTACT, SOISP_NUM, SOISP_DATE, SOISP_PERSON, ORDERNUM, INS_DATE, INS_WHO, UPD_DATE, UPD_WHO FROM REF_SOISP ";
            sql += "WHERE ISN_DOC_OUT=" + isn.ToString() + " ORDER BY ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("REF_SOISP");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetOrganiz754mss(connection, doc, dr.GetString(1), "ORGANIZ");
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(2))
                //{
                //    tag = doc.CreateElement("ISN_CONTACT");
                //    tag.InnerText = dr.GetInt32(2).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("SOISP_NUM");
                    tag.InnerText = dr.GetString(3);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("SOISP_DATE");
                    tag.InnerText = dr.GetValue(4).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(5))
                {
                    tag = GetDepartment754mss(connection, doc, dr.GetString(5), "SOISP_PERSON");
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("ORDERNUM");
                    tag.InnerText = dr.GetInt32(6).ToString();
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(7))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(7).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(8), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(9))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(9).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(10))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(10), "UPD_WHO");
                //    res.AppendChild(tag);
                //}
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetLink754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("LINKS");
            String sql = "SELECT RL.ISN_REF_LINK,RL.LINKED_NUM,null as URL_STR,RL.ORDERNUM,null as REG_COUNT,RL.INS_DATE,RL.INS_WHO,RL.UPD_DATE,RL.UPD_WHO"
                        +",LNK.ISN_LCLASSIF,LNK.CLASSIF_NAME,null as LINK_TYPE,null as LINK_DIR,LNK.DELETED,null as LINK_INDEX,LNK.NOTE,PLNK.ISN_LCLASSIF"
                        +",PLNK.CLASSIF_NAME,null as LINK_TYPE,null as LINK_INDEX,RC.ISN_DOC,RC.DUE_DOCGROUP,RC.FREE_NUM,RC.DOC_DATE "
                        +"FROM REF_LINK AS RL INNER JOIN LINK_CL AS LNK ON RL.ISN_CLLINK = LNK.ISN_LCLASSIF LEFT OUTER JOIN "
                        +"(select ISN_DOC_INP as ISN_DOC,DUE_DOCGROUP,FREE_NUM,DOC_DATE FROM RC_INP union select ISN_DOC_OUT as ISN_DOC,DUE_DOCGROUP,FREE_NUM,DOC_DATE FROM RC_OUT) AS RC ON "
                        +"RL.ISN_LINKED_DOC = RC.ISN_DOC INNER JOIN LINK_CL AS PLNK ON LNK.ISN_PARE_LINK = PLNK.ISN_LCLASSIF "
                        +"WHERE RL.ISN_REF_DOC=" + isn.ToString() + " ORDER BY RL.ORDERNUM";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("LINK");
                XmlAttribute attr = doc.CreateAttribute("isn");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("LINKED_NUM");
                    tag.InnerText = dr.GetString(1);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("URL_STR");
                    tag.InnerText = dr.GetString(2);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("ORDERNUM");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(4))
                //{
                //    tag = doc.CreateElement("REG_COUNT");
                //    tag.InnerText = dr.GetInt32(4).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(5).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(6))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(6), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(7))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(7).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(8), "UPD_WHO");
                //    res.AppendChild(tag);
                //}
                XmlNode lnk = doc.CreateElement("LNK");
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ISN_LCLASSIF");
                    tag.InnerText = dr.GetInt32(9).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(10);
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(11))
                {
                    tag = doc.CreateElement("LINK_TYPE");
                    tag.InnerText = dr.GetInt32(11).ToString();
                    lnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(12))
                {
                    tag = doc.CreateElement("LINK_DIR");
                    tag.InnerText = dr.GetInt32(12).ToString();
                    lnk.AppendChild(tag);
                }
                //if (!dr.IsDBNull(13))
                //{
                //    tag = doc.CreateElement("DELETED");
                //    tag.InnerText = dr.GetInt32(13).ToString();
                //    lnk.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(14))
                //{
                //    tag = doc.CreateElement("LINK_INDEX");
                //    tag.InnerText = dr.GetString(14);
                //    lnk.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(15))
                //{
                //    tag = doc.CreateElement("NOTE");
                //    tag.InnerText = dr.GetString(15);
                //    lnk.AppendChild(tag);
                //}
                XmlNode plnk = doc.CreateElement("PLNK");
                if (!dr.IsDBNull(16))
                {
                    tag = doc.CreateElement("ISN_LCLASSIF");
                    tag.InnerText = dr.GetInt32(16).ToString();
                    plnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(17))
                {
                    tag = doc.CreateElement("CLASSIF_NAME");
                    tag.InnerText = dr.GetString(17);
                    plnk.AppendChild(tag);
                }
                if (!dr.IsDBNull(18))
                {
                    tag = doc.CreateElement("LINK_TYPE");
                    tag.InnerText = dr.GetInt32(18).ToString();
                    plnk.AppendChild(tag);
                }
                //if (!dr.IsDBNull(19))
                //{
                //    tag = doc.CreateElement("LINK_INDEX");
                //    tag.InnerText = dr.GetString(19);
                //    plnk.AppendChild(tag);
                //}
                lnk.AppendChild(plnk);
                res.AppendChild(lnk);
                XmlNode rc = doc.CreateElement("RC");
                if (!dr.IsDBNull(20))
                {
                    tag = doc.CreateElement("ISN_DOC");
                    tag.InnerText = dr.GetInt32(20).ToString();
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(21))
                {
                    tag = doc.CreateElement("DUE_DOCGROUP");
                    tag.InnerText = dr.GetString(21);
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(22))
                {
                    tag = doc.CreateElement("FREE_NUM");
                    tag.InnerText = dr.GetString(22);
                    rc.AppendChild(tag);
                }
                if (!dr.IsDBNull(23))
                {
                    tag = doc.CreateElement("DOC_DATE");
                    tag.InnerText = dr.GetValue(23).ToString();
                    rc.AppendChild(tag);
                }
                res.AppendChild(rc);
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetSend754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("SENDS");
            String sql = "SELECT ISN_REF_SEND,ISN_CITIZEN,DUE_DEP,ISN_REESTR,SEND_PERSON,ORDERNUM,ISN_DELIVERY,SEND_DATE"
                        +",ISN_SENDUSER,CONSISTS,null as ISN_CONTACT,DUE_ORGANIZ,FLAG_ORIGINAL,NOTE,null as SENDING_TYPE"
                        +",null as REG_N,null as REG_DATE,null as ANSWER,null as ANSWER_DATE,INS_DATE,INS_WHO,UPD_DATE"
                        +",UPD_WHO FROM REF_SEND WHERE ISN_REF_DOC =" + isn.ToString() + " ORDER BY ORDERNUM ";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("SEND");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = doc.CreateElement("ISN_CITIZEN");
                    tag.InnerText = dr.GetInt32(1).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(2))
                {
                    tag = doc.CreateElement("DUE_DEP");
                    tag.InnerText = dr.GetString(2);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(3))
                //{
                //    tag = doc.CreateElement("ISN_REESTR");
                //    tag.InnerText = dr.GetInt32(3).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(4))
                {
                    tag = doc.CreateElement("SEND_PERSON");
                    tag.InnerText = dr.GetString(4);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(5))
                //{
                //    tag = doc.CreateElement("ORDERNUM");
                //    tag.InnerText = dr.GetInt32(5).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(6))
                {
                    tag = GetDelivery754mss(connection, doc, dr.GetInt32(6));
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(7))
                //{
                //    tag = doc.CreateElement("SEND_DATE");
                //    tag.InnerText = dr.GetValue(7).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("ISN_SENDUSER");
                //    tag.InnerText = dr.GetInt32(8).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(9))
                //{
                //    tag = doc.CreateElement("CONSISTS");
                //    tag.InnerText = dr.GetString(9);
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(10))
                //{
                //    tag = doc.CreateElement("ISN_CONTACT");
                //    tag.InnerText = dr.GetInt32(10).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(11))
                {
                    tag = GetOrganiz754mss(connection, doc, dr.GetString(11), "DUE_ORGANIZ");
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(12))
                //{
                //    tag = doc.CreateElement("FLAG_ORIGINAL");
                //    tag.InnerText = dr.GetInt32(12).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(14))
                //{
                //    tag = doc.CreateElement("SENDING_TYPE");
                //    tag.InnerText = dr.GetInt32(14).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(15))
                //{
                //    tag = doc.CreateElement("REG_N");
                //    tag.InnerText = dr.GetString(15);
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(16))
                //{
                //    tag = doc.CreateElement("REG_DATE");
                //    tag.InnerText = dr.GetValue(16).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(17))
                //{
                //    tag = doc.CreateElement("ANSWER");
                //    tag.InnerText = dr.GetInt32(17).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(18))
                //{
                //    tag = doc.CreateElement("ANSWER_DATE");
                //    tag.InnerText = dr.GetValue(18).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(19))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(19).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(20))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(20), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(21))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(21).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(22))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(22), "UPD_WHO");
                //    res.AppendChild(tag);
                //}
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        private static XmlNode GetCorresp754mss(OleDbConnection connection, XmlDocument doc, Int32 isn)
        {
            XmlNode result = doc.CreateElement("CORRESPS");
            String sql = "SELECT ISN_REF_CORRESP, DUE_ORGANIZ,null as ISN_CONTACT, CORRESP_KIND, ORDERNUM, CORRESP_DATE, CORRESP_NUM, CORRESP_SIGN, TEL_NUM"
                        +",ANNOTAT, DOC_NAME, CONSISTS, ANNOTAT_1, NOTE, NOTE_1, INS_DATE, INS_WHO, UPD_DATE, UPD_WHO FROM REF_CORRESP "
                        +"WHERE ISN_DOC_INP =" + isn.ToString() + " ORDER BY ORDERNUM ";
            OleDbCommand cmd = new OleDbCommand(sql, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                XmlNode res = doc.CreateElement("CORRESP");
                XmlAttribute attr = doc.CreateAttribute("ISN");
                attr.Value = dr.GetInt32(0).ToString();
                res.Attributes.Append(attr);
                XmlNode tag;
                if (!dr.IsDBNull(1))
                {
                    tag = GetOrganiz754mss(connection, doc, dr.GetString(1), "DUE_ORGANIZ");
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(2))
                //{
                //    tag = doc.CreateElement("ISN_CONTACT");
                //    tag.InnerText = dr.GetInt32(2).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(3))
                {
                    tag = doc.CreateElement("CORRESP_KIND");
                    tag.InnerText = dr.GetInt32(3).ToString();
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(4))
                //{
                //    tag = doc.CreateElement("ORDERNUM");
                //    tag.InnerText = dr.GetInt32(4).ToString();
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(5))
                {
                    tag = doc.CreateElement("CORRESP_DATE");
                    tag.InnerText = dr.GetValue(5).ToString();
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(6))
                {
                    tag = doc.CreateElement("CORRESP_NUM");
                    tag.InnerText = dr.GetString(6);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(7))
                {
                    tag = doc.CreateElement("CORRESP_SIGN");
                    tag.InnerText = dr.GetString(7);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(8))
                //{
                //    tag = doc.CreateElement("TEL_NUM");
                //    tag.InnerText = dr.GetString(8);
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(9))
                {
                    tag = doc.CreateElement("ANNOTAT");
                    tag.InnerText = dr.GetString(9);
                    res.AppendChild(tag);
                }
                if (!dr.IsDBNull(10))
                {
                    tag = doc.CreateElement("DOC_NAME");
                    tag.InnerText = dr.GetString(10);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(11))
                //{
                //    tag = doc.CreateElement("CONSISTS");
                //    tag.InnerText = dr.GetString(11);
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(12))
                //{
                //    tag = doc.CreateElement("ANNOTAT_1");
                //    tag.InnerText = dr.GetString(12);
                //    res.AppendChild(tag);
                //}
                if (!dr.IsDBNull(13))
                {
                    tag = doc.CreateElement("NOTE");
                    tag.InnerText = dr.GetString(13);
                    res.AppendChild(tag);
                }
                //if (!dr.IsDBNull(14))
                //{
                //    tag = doc.CreateElement("NOTE_1");
                //    tag.InnerText = dr.GetString(14);
                //    res.AppendChild(tag);
                //}

                //if (!dr.IsDBNull(15))
                //{
                //    tag = doc.CreateElement("INS_DATE");
                //    tag.InnerText = dr.GetValue(15).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(16))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(16), "INS_WHO");
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(17))
                //{
                //    tag = doc.CreateElement("UPD_DATE");
                //    tag.InnerText = dr.GetValue(17).ToString();
                //    res.AppendChild(tag);
                //}
                //if (!dr.IsDBNull(18))
                //{
                //    tag = GetUser754mss(connection, doc, dr.GetInt32(18), "UPD_WHO");
                //    res.AppendChild(tag);
                //}
                result.AppendChild(res);
            }
            dr.Close();
            return result;
        }
        #endregion 7.54 Версия Oracle
    }
}
